import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.JSplitPane;
import javax.swing.JCheckBox;
import javax.swing.JFormattedTextField;
import javax.swing.JToggleButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Custom extends JDialog {

  private final JPanel contentPanel = new JPanel();
  private javax.swing.ButtonGroup buttonGroup1;
  private JCheckBox chckbxEveryDay;
  private JCheckBox chckbxEveryWeek;
  private JCheckBox chckbxEveryMonth;
  private JCheckBox chckbxEveryYear;
  private JFormattedTextField everyDaysText;
  private JFormattedTextField everyWeeksText;
  private JFormattedTextField everyMonthsText;
  private JFormattedTextField everyYearsText;
  private JToggleButton sunday;
  private JToggleButton monday;
  private JToggleButton tuesday;
  private JToggleButton wensday;
  private JToggleButton thursday;
  private JToggleButton friday;
  private JToggleButton saturday;
  
  private JToggleButton t1;
  private JToggleButton t2;
  private JToggleButton t3;
  private JToggleButton t4;
  private JToggleButton t5;
  private JToggleButton t6;
  private JToggleButton t7;
  private JToggleButton t8;
  private JToggleButton t9;
  private JToggleButton t10;
  private JToggleButton t11;
  private JToggleButton t12;
  private JToggleButton t13;
  private JToggleButton t14;
  private JToggleButton t15;
  private JToggleButton t16;
  private JToggleButton t17;
  private JToggleButton t18;
  private JToggleButton t19;
  private JToggleButton t20;
  private JToggleButton t21;
  private JToggleButton t22;
  private JToggleButton t23;
  private JToggleButton t24;
  private JToggleButton t25;
  private JToggleButton t26;
  private JToggleButton t27;
  private JToggleButton t28;
  private JToggleButton t29;
  private JToggleButton t30;
  private JToggleButton t31;

  private JToggleButton m1 ;
  private JToggleButton m2 ;
  private JToggleButton m3 ;
  private JToggleButton m4 ;
  private JToggleButton m5 ;
  private JToggleButton m6 ;
  private JToggleButton m7 ;
  private JToggleButton m8 ;
  private JToggleButton m9 ;
  private JToggleButton m10 ;
  private JToggleButton m11 ;
  private JToggleButton m12 ;

  private String finalRecurrentFormatedString="";
  
  //private CalendarApplication c;
  
  

public Custom() {
	buttonGroup1= new ButtonGroup();
	//c = new CalendarApplication();

	setBounds(100, 100, 800, 537);
	getContentPane().setLayout(new BorderLayout());
	contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    contentPanel.setLayout(null);
    
	JSeparator separator_1 = new JSeparator();
	separator_1.setBounds(0, 191, 774, 13);
	contentPanel.add(separator_1);
	
	JSeparator separator = new JSeparator();
	separator.setBounds(347, 7, 20, 466);
	separator.setOrientation(SwingConstants.VERTICAL);
	contentPanel.add(separator);
	
	chckbxEveryDay = new JCheckBox("Every Day");
	chckbxEveryDay.setBounds(10, 7, 97, 23);
  	contentPanel.add(chckbxEveryDay);

        chckbxEveryWeek = new JCheckBox("Every Week");
        chckbxEveryWeek.setBounds(379, 7, 124, 23);
        contentPanel.add(chckbxEveryWeek);

        chckbxEveryMonth = new JCheckBox("Every Month");
        chckbxEveryMonth.setBounds(10, 211, 156, 23);
        contentPanel.add(chckbxEveryMonth);

        chckbxEveryYear = new JCheckBox("Every Year");
        chckbxEveryYear.setBounds(377, 211, 97, 23);
        contentPanel.add(chckbxEveryYear);

        JLabel lblNewLabel = new JLabel("Every");
        lblNewLabel.setBounds(77, 103, 46, 14);
        contentPanel.add(lblNewLabel);

        everyDaysText = new JFormattedTextField();
        everyDaysText.setBounds(120, 100, 46, 20);
        everyDaysText.setText("1");
        contentPanel.add(everyDaysText);

        JLabel lblDaily = new JLabel("Day(s)");
        lblDaily.setBounds(176, 103, 46, 14);
        contentPanel.add(lblDaily);

        JLabel label = new JLabel("Every");
        label.setBounds(479, 90, 46, 14);
        contentPanel.add(label);

        everyWeeksText = new JFormattedTextField();
        everyWeeksText.setBounds(524, 87, 46, 20);
        everyWeeksText.setText("1");
        contentPanel.add(everyWeeksText);

        JLabel lblWeeksOn = new JLabel("Week(s) on:");
        lblWeeksOn.setBounds(589, 90, 77, 14);
        contentPanel.add(lblWeeksOn);

        sunday = new JToggleButton("S");
        sunday.setBounds(381, 136, 54, 43);
        contentPanel.add(sunday);

        monday = new JToggleButton("M");
        monday.setBounds(435, 136, 54, 43);
        contentPanel.add(monday);

        tuesday = new JToggleButton("T");
        tuesday.setBounds(489, 136, 52, 43);
        contentPanel.add(tuesday);

        wensday = new JToggleButton("W");
        wensday.setBounds(541, 136, 52, 43);
        contentPanel.add(wensday);

        thursday = new JToggleButton("T");
        thursday.setBounds(593, 136, 52, 43);
        contentPanel.add(thursday);

        friday = new JToggleButton("F");
        friday.setBounds(645, 136, 52, 43);
        contentPanel.add(friday);

        saturday = new JToggleButton("S");
        saturday.setBounds(697, 136, 54, 43);
        contentPanel.add(saturday);

        JLabel label_1 = new JLabel("Every");
        label_1.setBounds(77, 258, 46, 14);
        contentPanel.add(label_1);

        everyMonthsText = new JFormattedTextField();
        everyMonthsText.setBounds(120, 255, 46, 20);
        everyMonthsText.setText("1");
        contentPanel.add(everyMonthsText);

        JLabel lblMonths = new JLabel("month(s) each:");
        lblMonths.setBounds(179, 258, 102, 14);
        contentPanel.add(lblMonths);

        t2 = new JToggleButton("2");
        t2.setBounds(50, 283, 50, 35);
        t2.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t2);

        t1 = new JToggleButton("1");
        t1.setBounds(0, 283, 50, 35);
        t1.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t1);

         t3 = new JToggleButton("3");
        t3.setBounds(100, 283, 50, 35);
        t3.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t3);

         t4 = new JToggleButton("4");
        t4.setBounds(150, 283, 50, 35);
        t4.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t4);

         t5 = new JToggleButton("5");
        t5.setBounds(200, 283, 50, 35);
        t5.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t5);

         t6 = new JToggleButton("6");
        t6.setBounds(250, 283, 50, 35);
        t6.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t6);

         t7 = new JToggleButton("7");
        t7.setBounds(300, 283, 50, 35);
        t7.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t7);

         t14 = new JToggleButton("14");
        t14.setBounds(300, 317, 50, 35);
        t14.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t14);

         t13 = new JToggleButton("13");
        t13.setBounds(250, 317, 50, 35);
        t13.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t13);

         t12 = new JToggleButton("12");
        t12.setBounds(200, 317, 50, 35);
        t12.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t12);

         t11 = new JToggleButton("11");
        t11.setBounds(150, 317, 50, 35);
        t11.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t11);

         t10 = new JToggleButton("10");
        t10.setBounds(100, 317, 50, 35);
        t10.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t10);

         t9 = new JToggleButton("9");
        t9.setBounds(50, 317, 50, 35);
        t9.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t9);

         t8 = new JToggleButton("8");
        t8.setBounds(0, 317, 50, 35);
        t8.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t8);

         t21 = new JToggleButton("21");
        t21.setBounds(300, 352, 50, 35);
        t21.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t21);

         t20 = new JToggleButton("20");
        t20.setBounds(250, 352, 50, 35);
        t20.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t20);

         t19 = new JToggleButton("19");
        t19.setBounds(200, 352, 50, 35);
        t19.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t19);

         t18 = new JToggleButton("18");
        t18.setBounds(150, 352, 50, 35);
        t18.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t18);

         t17 = new JToggleButton("17");
        t17.setBounds(100, 352, 50, 35);
        t17.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t17);

         t16 = new JToggleButton("16");
        t16.setBounds(50, 352, 50, 35);
        t16.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t16);

         t15 = new JToggleButton("15");
        t15.setBounds(0, 352, 50, 35);
        t15.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t15);

         t28 = new JToggleButton("28");
        t28.setBounds(300, 387, 50, 35);
        t28.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t28);

         t27 = new JToggleButton("27");
        t27.setBounds(250, 387, 50, 35);
        t27.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t27);

         t26 = new JToggleButton("26");
        t26.setBounds(200, 387, 50, 35);
        t26.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t26);

         t25 = new JToggleButton("25");
        t25.setBounds(150, 387, 50, 35);
        t25.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t25);

         t24 = new JToggleButton("24");
        t24.setBounds(100, 387, 50, 35);
        t24.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t24);

         t23 = new JToggleButton("23");
        t23.setBounds(50, 387, 50, 35);
        t23.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t23);

         t22 = new JToggleButton("22");
        t22.setBounds(0, 387, 50, 35);
        t22.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t22);

         t31 = new JToggleButton("31");
        t31.setBounds(100, 420, 50, 35);
        t31.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t31);

         t30 = new JToggleButton("30");
        t30.setBounds(50, 420, 50, 35);
        t30.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t30);

         t29 = new JToggleButton("29");
        t29.setBounds(0, 420, 50, 35);
        t29.setFont(new Font("Tahoma", Font.PLAIN, 9));
        contentPanel.add(t29);

        JLabel label_2 = new JLabel("Every");
        label_2.setBounds(479, 258, 46, 14);
        contentPanel.add(label_2);

        everyYearsText = new JFormattedTextField();
        everyYearsText.setText("1");
        everyYearsText.setBounds(524, 255, 46, 20);
        contentPanel.add(everyYearsText);

        JLabel lblYears = new JLabel("year(s) in:");
        lblYears.setBounds(589, 258, 64, 14);
        contentPanel.add(lblYears);

         m1 = new JToggleButton("Jan");
        m1.setBounds(438, 287, 65, 50);
        contentPanel.add(m1);

         m2 = new JToggleButton("Feb");
        m2.setBounds(502, 287, 65, 50);
        contentPanel.add(m2);

         m3 = new JToggleButton("Mar");
        m3.setBounds(566, 287, 65, 50);
        contentPanel.add(m3);

         m5 = new JToggleButton("May");
        m5.setBounds(438, 338, 65, 50);
        contentPanel.add(m5);

         m6 = new JToggleButton("Jun");
        m6.setBounds(502, 338, 65, 50);
        contentPanel.add(m6);

         m7 = new JToggleButton("Jul");
        m7.setBounds(566, 338, 65, 50);
        contentPanel.add(m7);

         m4 = new JToggleButton("Apr");
        m4.setBounds(631, 287, 65, 50);
        contentPanel.add(m4);

         m8 = new JToggleButton("Aug");
        m8.setBounds(631, 338, 65, 50);
        contentPanel.add(m8);

         m9 = new JToggleButton("Sep");
        m9.setBounds(438, 389, 65, 50);
        contentPanel.add(m9);

         m10 = new JToggleButton("Oct");
        m10.setBounds(502, 389, 65, 50);
        contentPanel.add(m10);

         m11 = new JToggleButton("Nov");
        m11.setBounds(566, 389, 65, 50);
        contentPanel.add(m11);

         m12 = new JToggleButton("Dec");
        m12.setBounds(631, 389, 65, 50);
        contentPanel.add(m12);
        {
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
        getContentPane().add(buttonPane, BorderLayout.SOUTH);
        {
            JButton okButton = new JButton("save");
            
            okButton.addMouseListener(new MouseAdapter() {
            	@Override
            	public void mousePressed(MouseEvent arg0) {
            		
            		finalRecurrentFormatedString = getRecurrentDetail();
            		if(!finalRecurrentFormatedString.equals("")){
            			setVisible(false);
            			CalendarApplication c = new CalendarApplication();
            			c.setRecurrentString(finalRecurrentFormatedString);
            		}
            	}
            });
            okButton.setActionCommand("OK");
            buttonPane.add(okButton);
            getRootPane().setDefaultButton(okButton);
        }
        {
            JButton cancelButton = new JButton("Cancel");
            cancelButton.addActionListener(new ActionListener() {
            	public void actionPerformed(ActionEvent e) {
            		setVisible(false);
            	}
            });
            cancelButton.setActionCommand("Cancel");
            buttonPane.add(cancelButton);
        }
        }
        this.groupUpButtons();
        chckbxEveryDay.setSelected(true);
        
        this.setLocationRelativeTo(null);
}
  public void groupUpButtons(){
        buttonGroup1.add(chckbxEveryDay);
        buttonGroup1.add(chckbxEveryWeek);
        buttonGroup1.add(chckbxEveryMonth);
        buttonGroup1.add(chckbxEveryYear);
  }
  public String getSelectedWeekDayString(){  //SU,MO,TU,WE,TH,FR,SA
	  String weekString ="";
	  
	  if(sunday.isSelected()){
		  weekString +="SU,";
	  }
	  if(monday.isSelected()){
		  weekString +="MO,";
	  }
	  if(tuesday.isSelected()){
		  weekString +="TU,";
	  }
	  if(wensday.isSelected()){
		  weekString +="WE,";
	  }
	  if(thursday.isSelected()){
		  weekString +="TH,";
	  }
	  if(friday.isSelected()){
		  weekString +="FR,";
	  }
	  if(saturday.isSelected()){
		  weekString +="SA,";
	  }
	  
	  
	  if(!weekString.equals("")){
		  weekString = weekString.substring(0, weekString.length()-1);
	  }else{
		  JOptionPane.showMessageDialog(null, "Since you selected Repeat Weekly, please select at least one weekday", "Input Error", JOptionPane.ERROR_MESSAGE);
	  }
	  
	  return weekString;
  }
  public String getSelected31DayString(){
	  String dayString = "";
	  if(t1.isSelected()) {        dayString += t1.getText()+",";        }
	  if(t2.isSelected()) {        dayString += t2.getText()+",";        }
	  if(t3.isSelected()) {        dayString += t3.getText()+",";        }
	  if(t4.isSelected()) {        dayString += t4.getText()+",";        }
	  if(t5.isSelected()) {        dayString += t5.getText()+",";        }
	  if(t6.isSelected()) {        dayString += t6.getText()+",";        }
	  if(t7.isSelected()) {        dayString += t7.getText()+",";        }
	  if(t8.isSelected()) {        dayString += t8.getText()+",";        }
	  if(t9.isSelected()) {        dayString += t9.getText()+",";        }
	  if(t10.isSelected()){        dayString += t10.getText()+",";       }
	  if(t11.isSelected()){        dayString += t11.getText()+",";       }
	  if(t12.isSelected()){        dayString += t12.getText()+",";       }
	  if(t13.isSelected()){        dayString += t13.getText()+",";       }
	  if(t14.isSelected()){        dayString += t14.getText()+",";       }
	  if(t15.isSelected()){        dayString += t15.getText()+",";       }
	  if(t16.isSelected()){        dayString += t16.getText()+",";       }
	  if(t17.isSelected()){        dayString += t17.getText()+",";       }
	  if(t18.isSelected()){        dayString += t18.getText()+",";       }
	  if(t19.isSelected()){        dayString += t19.getText()+",";       }
	  if(t20.isSelected()){        dayString += t20.getText()+",";       }
	  if(t21.isSelected()){        dayString += t21.getText()+",";       }
	  if(t22.isSelected()){        dayString += t22.getText()+",";       }
	  if(t23.isSelected()){        dayString += t23.getText()+",";       }
	  if(t24.isSelected()){        dayString += t24.getText()+",";       }
	  if(t25.isSelected()){        dayString += t25.getText()+",";       }
	  if(t26.isSelected()){        dayString += t26.getText()+",";       }
	  if(t27.isSelected()){        dayString += t27.getText()+",";       }
	  if(t28.isSelected()){        dayString += t28.getText()+",";       }
	  if(t29.isSelected()){        dayString += t29.getText()+",";       }
	  if(t30.isSelected()){        dayString += t30.getText()+",";       }
	  if(t31.isSelected()){        dayString += t31.getText()+",";       }
	  
	  if(!dayString.equals("")){
		  dayString= dayString.substring(0, dayString.length()-1);
	  }else{
		  JOptionPane.showMessageDialog(null, "Since you selected Repeat Monthly, please select at least one Monthday", "Input Error", JOptionPane.ERROR_MESSAGE);
	  }
	  
	  
	  return dayString;
	  
  }
  
  public String getMonthString(){
	  String month ="";
	  if(m1.isSelected()) {         month += "1," ;      }
	  if(m2.isSelected()) {         month += "2," ;      } 
	  if(m3.isSelected()) {         month += "3," ;      }
	  if(m4.isSelected()) {         month += "4," ;      }
	  if(m5.isSelected()) {         month += "5," ;      }
	  if(m6.isSelected()) {         month += "6," ;      }
	  if(m7.isSelected()) {         month += "7," ;      }
	  if(m8.isSelected()) {         month += "8," ;      }
	  if(m9.isSelected()) {         month += "9," ;      }
	  if(m10.isSelected()){         month += "10,";      }
	  if(m11.isSelected()){         month += "11,";      }
	  if(m12.isSelected()){         month += "12,";      }
	 
	  
	  if(!month.equals("")){
		  month = month.substring(0, month.length()-1);
	  }else{
		  JOptionPane.showMessageDialog(null,  "Since you selected Repeat Yearly, please select at least one month", "Input Error", JOptionPane.ERROR_MESSAGE);
	  }
	  
	  
	  return month;
  }
  public String getRecurrentDetail(){
	  String interval  = ""; 
	  String frequence = "";
	  String byDay 	   = "";
	  String byMonthDay= "";
	  String byMonth   = "";
	  
	  if(chckbxEveryDay.isSelected()){
		  
		  try{
		  	int temp = Integer.parseInt(everyDaysText.getText());
		  	if(temp>1){
		  		interval  = "INTERVAL="+  everyDaysText.getText()  +  ";";
		  	}
		  	frequence = "FREQ=DAILY;";
			  
		  }catch(NumberFormatException e){
			  JOptionPane.showMessageDialog(null, "Please enter a correct number.", "alert", JOptionPane.ERROR_MESSAGE);

		  }
		  
		  
	  }
	  
	  else if(chckbxEveryWeek.isSelected()){
		  if(!getSelectedWeekDayString().equals("")){
			  try{
				  int temp = Integer.parseInt(everyWeeksText.getText());
				  if(temp>1){
					  interval  = "INTERVAL="		+  everyWeeksText.getText()    +  ";"   ;
				  }
			      frequence = "FREQ=WEEKLY;"                                            ;
				  byDay     = "BYDAY:"			+  getSelectedWeekDayString()  +  ";"   ;
			  }catch(NumberFormatException e){
				  JOptionPane.showMessageDialog(null, "Please enter a correct number.", "alert", JOptionPane.ERROR_MESSAGE);
			  }
		  }
	  }
	  
	  else if(chckbxEveryMonth.isSelected()){
		  if(!getSelected31DayString().equals("")){
			  try{
				  int temp = Integer.parseInt(everyMonthsText.getText());
				  if(temp>1){
					  interval  = "INTERVAL="+  everyMonthsText.getText()+  ";";
				  }
				  frequence = "FREQ=MONTHLY;";
				  byMonthDay= "BYMONTHDAY="+ getSelected31DayString()+  ";";
			  }catch(NumberFormatException e){
				  JOptionPane.showMessageDialog(null, "Please enter a correct number.", "alert", JOptionPane.ERROR_MESSAGE);
			  }
		  }
	  }
	  
	  else if(chckbxEveryYear.isSelected()){
		  if(!getMonthString().equals("")){
			  try{
				  int temp = Integer.parseInt(everyYearsText.getText());
				  if(temp>1){
					  interval  = "INTERVAL="+  everyYearsText.getText() +  ";";
				  }
				  frequence = "FREQ=YEARLY;";
				  byMonth   = "BYMONTH:" + getMonthString() + ";";
			  }catch(NumberFormatException e){
				  JOptionPane.showMessageDialog(null, "Please enter a correct number.", "alert", JOptionPane.ERROR_MESSAGE);

			  }
		  }
	  }
	  
	  
	  String recurrentString               = "RRULE:"+ frequence + interval  + byDay + byMonthDay + byMonth; 
	  
	  String eliminatedLastSamicolonString = recurrentString.substring(0, recurrentString.length()-1);
	  
	  
	  if(eliminatedLastSamicolonString.equals("RRULE")) {
		  eliminatedLastSamicolonString="";
	  }
	 
	  return eliminatedLastSamicolonString;
	  
  }// end of getRecurrentDetail();
  public String getRecurrentString(){
	  
	  return finalRecurrentFormatedString;
		
  }
}
